


package com.cloudminga.systemtray;


import java.awt.*;
import java.awt.event.*;
import com.cloudminga.gui.*;
import com.globussoft.util.TimeChecker;
import com.globussoft.xml.SettingsXml;

public class SysTray
{
     MenuItem StartBackupItem=null;
     MenuItem SuspendItem=null;
     private String projectName="";

    public SysTray()
    {
        new Thread()
        {
            @Override
            public void run()
            {
              TimeChecker.autoUpload();
            }
        }.start();

       final TrayIcon trayIcon;
       projectName=SettingsXml.getProjectName();
        if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().getImage(
                   getClass().getResource("/images/traylogo.png"));


            MouseListener mouseListener = new MouseListener() {
                
                public void mouseClicked(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse clicked!");                 
                }
                public void mouseEntered(MouseEvent e) {
                    //System.out.println("Tray Icon - Mouse entered!");
                }
                public void mouseExited(MouseEvent e) {
                    //System.out.println("Tray Icon - Mouse exited!");
                }
                public void mousePressed(MouseEvent e) {
                    //System.out.println("Tray Icon - Mouse pressed!");
                }
                public void mouseReleased(MouseEvent e) {
                    //System.out.println("Tray Icon - Mouse released!");
                }

            };

            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if(e.getActionCommand().toString().equals("Exit"))
                    {
                    System.out.println("Exiting..");
                    System.exit(0);
                    }
                }
            };

            ActionListener statusListener = new ActionListener(){
              public void actionPerformed(ActionEvent e){
                    System.out.println("Status..");

                   if(e.getActionCommand().toString().equals("Status"))
                   {
                         MainDialog statusObj = MainDialog.getInstance();

                         statusObj.setVisible(true);
                    }
                }

            };
          
             ActionListener startBackupListener = new ActionListener(){
              public void actionPerformed(ActionEvent e){
                    System.out.println("Start Backup..");

                   if(e.getActionCommand().toString().equals("Start Backup"))
                   {
                         StartBackupItem.setLabel("Cancel Backup");
                         System.out.println("Start Backup..");
                         MainDialog.setUploadFrom("selectedFiles backupSet");
                         MainDialog main= MainDialog.getInstance();
                         main.setVisible(true);
                    }
                  else if(e.getActionCommand().toString().equals("Cancel Backup"))
                  {

                      StartBackupItem.setLabel("Start Backup");
            
                    //  StartBackupItem.set
                       
                   }
                }

            };

             ActionListener configureListener = new ActionListener(){
                @Override
              public void actionPerformed(ActionEvent e){
                   

                   if(e.getActionCommand().toString().equals("Configure"))
                   {
                       Thread runner=new Thread()
                       {
                           public void run()
                           {
                             if(!MainForm.isFormOpened()){
                             MainDialog dialogMain = MainDialog.getInstance();
                             dialogMain.checkconfiguration();
                             }
                           }
                       };
                       runner.start();
//                       MainForm configureObj = new MainForm();
//                       //  configureObj.setVisible(true);
//                       configureObj.Restore_bt.setVisible(false);
                    }
                }
             };


            
             ActionListener restoreListener = new ActionListener(){
              public void actionPerformed(ActionEvent e){
                    System.out.println("Restore Files..");

                   if(e.getActionCommand().toString().equals("Restore Files"))
                   {

                         
                         MainForm restoreObj = new MainForm();
                         restoreObj.setVisible(true);
                         restoreObj.Restore_bt.setVisible(false);
                    }
                }

            };


             ActionListener suspendListener = new ActionListener(){
              public void actionPerformed(ActionEvent e){
                    System.out.println("Suspend..");

                   if(e.getActionCommand().toString().equals("Suspend"))
                   {

                        
                         MainForm restoreObj = new MainForm();
                         restoreObj.setVisible(true);

                    }
                }

            };
            
            PopupMenu popup = new PopupMenu();
           

            MenuItem StatusItem = new MenuItem("Status");
            StatusItem.addActionListener(statusListener);
            popup.add(StatusItem);
            popup.addSeparator();

            StartBackupItem = new MenuItem("Start Backup");
            StartBackupItem.addActionListener(startBackupListener);
            popup.add(StartBackupItem);

           // StartBackupItem.setLabel("Cancel Backup");

            MenuItem RestoreFilesItem = new MenuItem("Restore Files");
            RestoreFilesItem.addActionListener(restoreListener);
            popup.add(RestoreFilesItem);
            popup.addSeparator();

            MenuItem ConfigureItem = new MenuItem("Configure");
            ConfigureItem.addActionListener(configureListener);
            popup.add(ConfigureItem);

            SuspendItem = new MenuItem("Suspend");
            SuspendItem.addActionListener(suspendListener);
            popup.add(SuspendItem);
            popup.addSeparator();

            MenuItem HelpItem = new MenuItem("Help");
            HelpItem.addActionListener(exitListener);
            popup.add(HelpItem);

            MenuItem AboutItem = new MenuItem("About");
            StatusItem.addActionListener(exitListener);
            popup.add(AboutItem);


            MenuItem ExitItem = new MenuItem("Exit");
            ExitItem.addActionListener(exitListener);
            popup.add(ExitItem);

            trayIcon = new TrayIcon(image, projectName+" - Peace of Mind.", popup);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    trayIcon.displayMessage(projectName+" - Peace of Mind.",
                        "Backup your files automatically!",
                        TrayIcon.MessageType.INFO);
                }
            };
            
            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(actionListener);
            trayIcon.addMouseListener(mouseListener);

            //    Depending on which Mustang build you have, you may need to uncomment
            //    out the following code to check for an AWTException when you add 
            //    an image to the system tray.

                try {
                      tray.add(trayIcon);
             } catch (AWTException e) {
                   System.err.println("TrayIcon could not be added.");
                }

        } else {
            System.err.println("System tray is currently not supported.");
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        SysTray sysTray = new SysTray();
        Logger.LogInConsole(true);
        MainDialog dialogMain = MainDialog.getInstance();
        dialogMain.checkconfiguration();
       // dialogMain.setVisible(true);                        

    }
    
}
